﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace UIFrameWork
{
	public abstract class Singleton<T> where T:class ,new()
	{
		private static T s_instance;

		public static T instance
		{
			get
			{
				if (Singleton<T>.s_instance == null)
				{
					CreateInstance();
				}
				return s_instance;
			}
		}

		protected Singleton()
		{
			
		}

		public static void CreateInstance()
		{
			if (s_instance == null)
			{
				s_instance = Activator.CreateInstance<T>();
				(s_instance as Singleton<T>).Init();
			}
		}

		public static void DestoryInstance()
		{
			if (s_instance != null)
			{
				(s_instance as Singleton<T>).UnInit();
				s_instance = null;
			}
		}

		public static bool IsHaveInstance()
		{
			return s_instance != null;
		}
		public static T GetInstance()
		{
			if (Singleton<T>.s_instance == null)
			{
				CreateInstance();
			}
			return s_instance;
		}
		public virtual void Init(){}
		public virtual void UnInit(){}
	}
}
